
//
//  DateConvert.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/23.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "DateConvert.h"

@implementation DateConvert

/*
 @brief 将日期串格式化为日期类型
 @param dateString 输入时间字符串
 */
+ (NSDate *)dateFromString:(NSString *)dateString
{
    NSDate *destDate;
    if (!dateString || [dateString isEqualToString:@""])
    {
        destDate = [NSDate date];
    }else
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat: @"yyyy-MM-dd"];
        destDate = [dateFormatter dateFromString:dateString];
        if (destDate  == nil)
        {
            [dateFormatter setDateFormat: @"yyyy/MM/dd"];
            destDate = [dateFormatter dateFromString:dateString];
        }
        if (destDate == nil) {
            [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
            destDate = [dateFormatter dateFromString:dateString];
        }
    }
    
    return destDate;
}


/*
 @brief 将日期串格式化为中式日期类型
 @param dateString 输入时间字符串
 */
+ (NSDate *)dateFromStringFormatChineseDate:(NSString *)dateString
{
    NSDate *destDate;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    if (!dateString || [dateString isEqualToString:@""])
    {
        destDate = [NSDate date];
    }else
    {
        destDate = [dateFormatter dateFromString:dateString];
    }
    
    return destDate;
}

/*
 @brief 将日期类型格式化为日期串
 @param date 日期
 */
+ (NSString *)stringFromDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    if (destDateString == nil )
    {
        [dateFormatter setDateFormat:@"yyyy/MM/dd"];
        destDateString = [dateFormatter stringFromDate:date];
    }
    return destDateString;
}


/*
 @brief 将日期类型格式化为标准日期串
 @param date 日期
 */
+ (NSString *)stringFromDateFormatDefault:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}

/*
 @brief 将日期类型格式化为日期串
 @param date 日期
 */
+ (NSString *)stringFromDateFormatYYMMDD:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    if (destDateString == nil )
    {
        [dateFormatter setDateFormat:@"yyyy/MM/dd"];
        destDateString = [dateFormatter stringFromDate:date];
    }
    return destDateString;
}

/*
 @brief 将日期类型格式化为中式日期串
 @param date 日期
 */
+ (NSString *)stringFromDateFormatChineseDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}

/*
 @brief 将日期字符串格式化为yyyy-MM-dd HH:mm:ss
 @param time 日期格式字符串
 */
+ (NSString *)stringFormatDefaultTime:(NSString *)time
{
    NSString *formatTime = @"";
    if (time.length<10) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        formatTime = [dateFormatter stringFromDate:[NSDate date]];
    }
    else
    {
        formatTime = time;
    }
    
    return formatTime;
}


/*
 @brief 将日期类型格式化为标准日期串YYYY-MM-DD hh:mm
 @param date 日期
 */
+ (NSString *)stringFromMinuteFormat:(NSString *)dateStr {
    NSDate *date = [self dateFromString:dateStr];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}

/*
 @brief 将日期字符串格式化为YYYY-MM-DD
 @param time 日期格式字符串
 */
+ (NSString *)stringFormatTime:(NSString *)time
{
    NSString *formatTime = @"";
    if (time.length<10) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        formatTime = [dateFormatter stringFromDate:[NSDate date]];
    }
    else
    {
        formatTime = [time substringToIndex:10];
    }
    
    return formatTime;
}

+ (NSString *)stringFromTimeIntervalYYMMDD:(NSString *)timeInterval {
    double dou = [timeInterval doubleValue]/1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dou];
    NSString *str = [self stringFromDateFormatYYMMDD:date];
    return str;
}



+ (NSString *)stringFromTimeInterval:(NSString *)timeInterval
{
   //如果是毫秒要除1000
    double dou = [timeInterval doubleValue]/1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dou];
    NSString *str = [self stringFromDateFormatDefault:date];
    return str;
}

/*
 @brief 将日期格式化为MM/DD
 @param time 日期格式字符串
 */
+ (NSString *)MMDDstringFormatDate:(NSDate *)time
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM/dd"];
    NSString *destDateString = [dateFormatter stringFromDate:time];
    return destDateString;
    
}

/*
 @brief 将日期格式化为YYYY/MM/DD
 @param time 日期格式字符串
 */
+ (NSString *)YYYYMMDDstringFormatDate:(NSDate *)time
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY/MM/dd"];
    NSString *destDateString = [dateFormatter stringFromDate:time];
    return destDateString;
    
}

//1小时内显示几分钟前 一天内显示几小时内 超过一天显示02/20 跨年显示2012/02/20
+ (NSString *)displayDateStr:(NSString *)timeInterval {
    double dou = [timeInterval doubleValue]/1000;
    NSDate *dd = [NSDate dateWithTimeIntervalSince1970:dou];
    NSDate *curDate = [NSDate date];
    NSTimeInterval val = [curDate timeIntervalSince1970];
    
    double dur = val - dou;
    if (dur > 0 && dur <SECONDS_IN_MINUTE) {
        return NSStringFormat(@"%d seconds ago",(int)dur);
    } else if (dur < SECONDS_IN_HOUR) {
        return NSStringFormat(@"%d minutes ago",(int)(dur/SECONDS_IN_MINUTE));
    } else if (dur < SECONDS_IN_DAY) {
        return NSStringFormat(@"%d hours ago",(int)(dur/SECONDS_IN_HOUR));
    } else  {
        if ([dd yearsAgo]) {
             return [self YYYYMMDDstringFormatDate:dd];
        } else {
            return [self MMDDstringFormatDate:dd];
        }
    }
    return @"0 mintues ago";
    
}
+(NSString *)getCurrentTimeInterval {
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval tmp = [date timeIntervalSince1970]*1000;
    NSString *timestamp = [NSString stringWithFormat:@"%f",tmp];
    return timestamp;
}

// 获取系统时间
+ (NSString *)getSystemDatetime {
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSString* date = [formatter stringFromDate:[NSDate date]];
    return date;
}

@end
